using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using System.Diagnostics;

namespace DMS.Features.ListProvisioning
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("0679e878-6ffa-4cc8-9cb5-bf4338249bba")]
    public class ListProvisioningEventReceiver : SPFeatureReceiver
    {
        // Uncomment the method below to handle the event raised after a feature has been activated.
        string invoiceSite = "Invoices";
        string gatsSite = "GATs";
        string creditApplication = "CreditApplications";


        /// <summary>
        /// Create List: GATs Custom Document Library in GATs Subsite
        /// Create List: CreditApplications Document Library in CreditApplications Subsite
        /// Create List: Invoice Document Library in Invoices Subsite
        /// Create List: Invoices List in Invoices Subsite
        /// </summary>
        /// <param name="properties"></param>
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPSite siteCollection = properties.Feature.Parent as SPSite;

            using (SPSite site = new SPSite(siteCollection.ID))
            {
                using (SPWeb dms = site.RootWeb)
                {
                    try
                    {
                        //create branches list in site collection
                        //SPListTemplate template = dms.ListTemplates["Branch List Definition"];
                        SPListTemplate invoiceListTemplate = dms.ListTemplates["Invoice Definition"];
                        SPListTemplate turnersDocTemplate = dms.ListTemplates["Turners Document Library"];
                        SPListTemplate docTemplate = dms.ListTemplates["Document Library"];
                            
                        SPListTemplate branchTemplate = dms.ListTemplates["Branch Definition"];

                        //EventLog.WriteEntry(sSource, sEvent);

                        SPSecurity.RunWithElevatedPrivileges(delegate() { 
                            //create Document Store in Root Site
                            if (dms.Lists.TryGetList("DocumentStore") == null)
                            {
                                Guid dsId = dms.Lists.Add("DocumentStore", "Document Store", turnersDocTemplate);

                            }
                            if (dms.Lists.TryGetList("Branches") == null)
                            {
                                Guid dsId = dms.Lists.Add("Branches", "Branch list", branchTemplate);

                            }



                            //create Credit Applicationis document library in Credit Applications subsite
                            SPWeb ca = dms.Webs[creditApplication];
                            if (ca.Lists.TryGetList("CreditApplications") == null)
                            {
                                Guid caDocId = ca.Lists.Add("CreditApplications", "My Turners Credit Application", turnersDocTemplate);
                            }
                            //create GATs document library in GATs subsite
                            SPWeb gats = dms.Webs[gatsSite];
                            if (gats.Lists.TryGetList("GATs") == null)
                            {
                                Guid gatsDocId = gats.Lists.Add("GATs", "My Turners GATs", turnersDocTemplate);
                            }

                            //create current year invoices subsite
                            /*string currentYear = DateTime.Now.Year.ToString();
                            SPWeb invoices = dms.Webs[invoiceSite].Webs[currentYear];
                            if (invoices != null)
                            {
                                if (invoices.Lists.TryGetList("InvoiceDocuments") == null)
                                {
                                    Guid invoicedocId = invoices.Lists.Add("InvoiceDocuments", "Invoice Documents", docTemplate);
                                }
                                if (invoices.Lists.TryGetList("Invoices") == null)
                                {
                                    Guid invoiceListId = invoices.Lists.Add("Invoices", "Invoices", invoiceListTemplate);
                                }
                            }     */                   
                        
                        });

                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
            }
        }


        // Uncomment the method below to handle the event raised before a feature is deactivated.

        //public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        //{
        //}


        // Uncomment the method below to handle the event raised after a feature has been installed.

        //public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        //{
        //}


        // Uncomment the method below to handle the event raised before a feature is uninstalled.

        //public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        //{
        //}

        // Uncomment the method below to handle the event raised when a feature is upgrading.

        //public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
        //{
        //}
    }
}
